package com.wangxin.services;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.ehcache.CacheKit;
import com.wangxin.model.LETTER;
import com.wangxin.model.SysAttachment;
import com.wangxin.model.SysDict;
import com.wangxin.model.TASK;
import com.wangxin.model.USER;
import com.wangxin.utils.CreateVerifyCode;
import com.wangxin.utils.CreateXfdjh;
import com.wangxin.utils.SnowflakeIdGenerator;

public class WxScves {

	private String dictCache = "dict_cache";
	private String dictMapCache = "dict_cache_map";
	
	public boolean addWxNote(final LETTER letter, final USER user){
		boolean rtn = false;
		rtn = Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				Date curr = new Date();
				//生成信访件编号
				Long letterId = SnowflakeIdGenerator.getNextId(1,1);
				String cxm = "MZ"+CreateVerifyCode.getCode(4);
				String xfjbh = CreateXfdjh.getXfdjh("XF_CODE_3");
				String slbh = CreateXfdjh.getXfdjh("XF_SL_CODE_0_3");
				letter.setId(letterId);
				letter.setFCentralId(xfjbh);
				letter.setCxm(cxm);
				letter.setSourceType(0);//0本级1信访局2民政部3国家信访局
				letter.setState(0);//状态，0未处理、1处理中、2已办结
				letter.setCategory(3);//分类，1办信2接访3网信5视频
				letter.setXfrq(curr);
				letter.setSlbh(slbh);
				letter.setCreatedDate(curr);
				Long userId = SnowflakeIdGenerator.getNextId(1,1);
				letter.setFirstUserId(userId);
				letter.setOrgId(1l);//默认广西民政厅
				user.setId(userId);
				user.setLetterId(letterId);
				user.setCreatedDate(curr);
				TASK task = new TASK();
				task.setId(SnowflakeIdGenerator.getNextId(1,1));
				task.setLetterId(letterId);
				task.setCompanyId(letter.getOrgId());
				task.setState(0);
				task.setCreatedDate(curr);
				letter.save();
				user.save();
				task.save();
				return true;
			}
			
		});
		return rtn;
	}
	
	public String saveImage(File file, String type, String uploadPath){
		SysAttachment img = new SysAttachment();
		img.setId(SnowflakeIdGenerator.getNextId(1,1));
		img.setFilename(file.getName());
		img.setFileExt(type);
		img.setFileSize(file.length());
		img.setState(0);
		img.setDownloadCount(0L);
		img.setCreatedDate(new Date());
		String parentPath = file.getParent();
		String newfileName = String.format("%s.%s.file", img.getId()+"", type);
		File newFile = new File(parentPath+File.separator+newfileName);
		file.renameTo(newFile);
		img.setFilePath(uploadPath+"/"+newfileName);
		img.save();
		return img.getId()+"";
	}
	
	public List<SysDict> getSysDicts(){
		List<SysDict> dicts = SysDict.dao.findByCache(dictCache, "allSysDicts", "SELECT * FROM SYS_DICT");
		return dicts;
	}
	
	public List<SysDict> getSysDictsByCategory(String category){
		Map<String, List<SysDict>> dictMaps = CacheKit.get(dictCache, dictMapCache);
		if(dictMaps == null){
			dictMaps = new HashMap<String, List<SysDict>>();
			List<SysDict> all = getSysDicts();
			for(SysDict dict : all){
				String categoryKey = dict.getCategory();
				if(!dictMaps.containsKey(categoryKey)){
					dictMaps.put(categoryKey, new ArrayList<SysDict>());
				}
				dictMaps.get(categoryKey).add(dict);
			}
			CacheKit.put(dictCache, dictMapCache, dictMaps);
		}
		return dictMaps.containsKey(category) ? dictMaps.get(category) : new ArrayList<SysDict>();
	}
	
}
